# -*- coding: utf-8 -*-
"""
Created on Sun Jul  4 07:47:11 2021

@author: zhuo，木鸟
"""

import numpy as np
import pandas as pd
from calculate_plate_index import plot_plate_index
from scipy.stats import pearsonr

if __name__ == '__main__':
    data = pd.read_excel("../附件/上证指数.xlsx")
    date = data['日期']
    base = data['收盘'].iloc[0]
    index = data['收盘']/base*1000
    index.index = date
    
    # 插入缺失的日期
    index_fillin = index.resample('D').mean()
    index_fillin.fillna(method='ffill', inplace=True)
    
    # 画图
    plot_plate_index(index_fillin, shanghai=True)
    # 保存数据
    index_fillin.to_excel('../附件/中间数据/上证指数.xlsx')
    # 计算相关系数
    shang_index = index_fillin.iloc[:-1]
    plate_index = pd.read_excel(r'../附件/中间数据/光伏-建筑板块市值.xlsx', index_col=0)
    
    corr, p_value = pearsonr(shang_index.values, plate_index.values.reshape(-1,))
    
    print('上证指数和光伏-建筑板块指数的相关性为： ', corr, '\n显著水平为： ', 1-p_value)